package v1

import (
	"zimuzu_cvn_web_api/config"
	"github.com/gin-gonic/gin"
		"zimuzu_cvn_web_api/router/api"
	"github.com/gin-gonic/gin/binding"
	"zimuzu_cvn_web_api/service"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/lib/geetest"
	"zimuzu_cvn_web_api/router/api/web/middleware"
	"errors"
)

type ValidateAPI struct {
}

func (validate ValidateAPI) Name() string {
	return "validate API"
}

func (validate ValidateAPI) Bind() {
	validateAPI := config.C.Router.Group("/v1/validate")
	{
		validateAPI.POST("getCode", getCode)
		validateAPI.GET("geetestInit", geetestInit)
		validateAPI.POST("verifyCode", verifyCode)
		validateAPI.POST("geetTestValidate", geetTestValidate)
	}
}

//极验初始化
//Auth: Xu
func geetestInit(c *gin.Context){
	response := geetest.GeetestInit(c, "web")

	errno.OK.Info = "获取成功"
	api.SendResponse(c, errno.OK, response)
	return
}
//极验验证
func geetTestValidate(c *gin.Context){
	//滑动验证
	_,validateErr := geetest.Validate(c, geetest.ClientTypeWEB)
	if validateErr != nil{
		api.SendResponse(c, validateErr, nil)
		return
	}else{
		errno.OK.Info = "验证成功"
		api.SendResponse(c, errno.OK, nil)
		return
	}
}

type regSendCodeParams struct{
	Mobile string  `form:"mobile" json:"mobile" binding:"required"`
	Scene string   `form:"scene" json:"scene" binding:"required"`
	Area int   `form:"area" json:"area"`
}
//发送验证码
//Auth: Xu
func getCode(c *gin.Context){
	var params regSendCodeParams
	err := c.ShouldBindWith(&params, binding.FormPost)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	//已登陆状态下使用已绑定的手机号
	if checkLoginErr := middleware.CheckTokenLogin(c); checkLoginErr == nil{
		uid,_ := service.GetUserIdByRequest(c)
		userInfo, _ := service.GetUserInfo(uid)
		params.Mobile = userInfo.PhoneNumber
	}

	if params.Scene == service.SendCodeSceneReg{
		//检查账号是否已注册
		res,_ := service.CheckAccountReg(params.Mobile, params.Area)
		if res {
			api.SendResponse(c, errors.New("账号已注册"), nil)
			return
		}
	}

	//滑动验证
	_,validateErr := geetest.Validate(c, geetest.ClientTypeWEB)
	if validateErr != nil{
		api.SendResponse(c, validateErr, nil)
		return
	}
	//发送短信
	if sendErr := service.SendCode(params.Mobile, params.Scene, params.Area); sendErr != nil{
		api.SendResponse(c, sendErr, nil)
		return
	}

	errno.OK.Info = "发送短信成功"
	api.SendResponse(c, errno.OK, nil)
}

type verifyRegCodeParams struct{
	Mobile string  `form:"mobile" json:"mobile" binding:"required"`
	Scene string   `form:"scene" json:"scene" binding:"required"`
	Code int   `form:"code" json:"code" binding:"required"`

}
//验证码校验
//Auth: Xu
func verifyCode(c *gin.Context){
	var params verifyRegCodeParams
	err := c.ShouldBindWith(&params, binding.FormPost)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}

	if errValid := service.ValidateCode(params.Mobile , params.Scene, params.Code); errValid != nil{
		api.SendResponse(c, errValid, nil)
		return
	}

	sign := service.CreateRegSign(params.Mobile)
	responseData := make(map[string]string)
	responseData["sign"] = sign

	errno.OK.Info = "验证通过"
	api.SendResponse(c, errno.OK, responseData)
}
